import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {StorageService} from '../../services/storage.service';

@Component({
  selector: 'app-details',
  templateUrl: './details.component.html',
  styleUrls: ['./details.component.scss']
})
export class DetailsComponent implements OnInit {
// 接收传来的商品key值
public key: any = '0';
// 接收请求来的商品数值列表
public goodlist: any[] = [];
// 评论区与详情区切换
public onshow = true;

// 商品数量
public num: number = 1;

// 购物车提示窗关闭
public inshow = true;
  constructor(  public router: ActivatedRoute, public http: HttpClient, public storage: StorageService) {
  }

  ngOnInit() {
    // 接收父级传来的key
    this.router.queryParams.subscribe((result: any) => {
      console.log(result);
      this.key = result.key;
    } );
    // 请求商品数据列表
    const pi = 'http://192.168.2.227:8080/goods/list';
    this.http.get(pi).subscribe((respnse: any) => {
      console.log(respnse.data.list);
      this.goodlist = respnse.data.list;
      console.log(this.goodlist);
    });
    this.http.get('http://192.168.2.227:8080/goods/detail').subscribe((respnse: any) => {
      console.log(respnse);
    });
    // /goods/detail
  }
  // 显示控制
  // 直降
  show() {
     this.onshow = true;
  }
  // 评论区
  show2() {
    this.onshow = false;
  }
  // 购物车
  show3() {
    this.inshow = !this.inshow;
  }

  // 数量加减
  add1() {
    if (this.num < 999) {
      this.num += 1;
    }
  }
  sub1() {
    if ( this.num > 1) {
      this.num -= 1;
    }
  }
  // 发送购物信息到购物车
  up() {
    console.log(this.storage.get('id'));

    const sub = {
      headers: new HttpHeaders({'Content-Type': 'application/json'})};

    this.http.post('http://192.168.2.227:8080/shoppingcart/add', { user_id: this.storage.get('id') ,
        goods_id: this.goodlist[this.key].g_id, goods_count: this.num},
      sub).subscribe((respnse: any) => {
        console.log(respnse);
        console.log(this.storage.get('id'));
        console.log(this.goodlist[this.key].g_id);
        console.log(this.num);
    });
  }
}
